package com.fengpanlong.util;

import java.lang.reflect.Field;

import javax.servlet.http.HttpServletRequest;

public class Form {
	public static void saveParam(HttpServletRequest request, Object o) {
		reflect(request, o);
	}
	private static void reflect(HttpServletRequest request, Object o) {
		Field[] fields = o.getClass().getDeclaredFields();
		for(Field field : fields) {
			field.setAccessible(true);
			String name = field.getName();
			String param = request.getParameter(name);
			if(param != null) {
				param = replace(param);
				try {
					field.set(o, param);
				} catch (IllegalArgumentException e) {
					e.printStackTrace();
				} catch (IllegalAccessException e) {
					e.printStackTrace();
				}
			}
		}
		
	}
	private static String replace(String param) {
		String str = param.replaceAll("<", "&lt;");
		str = str.replaceAll(">", "&gt;");
		str = str.replaceAll("\"", "'");
		return str;
	}
}
